package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.log.UserActiveLogDao;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.UserActiveLog;
import com.tbit.uqbike.client.service.EvenJoinDetailService;
import com.tbit.uqbike.client.service.UserActiveLogService;
import com.tbit.uqbike.client.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:00 2022/5/17
 */
@Service
public class UserActiveLogServiceImpl implements UserActiveLogService {

    @Autowired
    private UserActiveLogDao userActiveLogDao;
    @Autowired
    private EvenJoinDetailService evenJoinDetailService;
    @Autowired
    private UserService userService;

    @Override
    public UserActiveLog getByPhoneAndEventId(String phone, Integer eventId, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("eventId", eventId);
        map.put("type", type);
        return userActiveLogDao.getByPhoneAndEventId(map);
    }

    @Override
    public void add(String phone, Integer eventId, Integer type, Integer userId) {
        UserActiveLog userActiveLogDB = getByPhoneAndEventId(phone, eventId, type);
        if (userActiveLogDB != null) {
            return;
        }
        UserActiveLog userActiveLog = new UserActiveLog();
        userActiveLog.setAddTime(new Date());
        userActiveLog.setPhone(phone);
        userActiveLog.setEventId(eventId);
        userActiveLog.setType(type);
        userActiveLogDao.add(userActiveLog);

        /**添加用户性别等统计*/
        User user = userService.getByUserId(userId);
        evenJoinDetailService.add(eventId, type, user, 1);
    }
}
